/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.patpro.modelo.DAO.persistencia;

import com.patpro.modelo.DAO.interfaces.SemestreInterfaz;
import com.patpro.modelo.javabeans.SemestreAcademico;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**

 */
public class SemestreJDBCPersistence implements SemestreInterfaz {

    private JdbcTemplate plantilla;

    public JdbcTemplate getPlantilla() {
        return plantilla;
    }

    public void setPlantilla(JdbcTemplate plantilla) {
        this.plantilla = plantilla;
    }
    
    public List<SemestreAcademico> listarSemestres() {
        String sql = "SELECT T_Semestre_Academico_ID as id_semestre,descripcion,anio FROM t_semestre_academico ";
        return plantilla.query(
                        sql,new BeanPropertyRowMapper(SemestreAcademico.class));
    }

    public int actualizarSemestre(SemestreAcademico semestre) {

        String qry=(semestre.getId_semestre()>0)
                ?"update t_semestre_academico set descripcion=?, anio=? where T_Semestre_Academico_ID="+semestre.getId_semestre()
                :"insert into t_semestre_academico(descripcion,anio) values(?,?)";
        	 return plantilla.update (qry,
                           new Object[] {
                                 semestre.getDescripcion(),
                                 semestre.getAnio()
                              });
    }

    public int eliminarSemestre(SemestreAcademico semestre) {
        String eliminar="delete from t_semestre_academico where T_Semestre_Academico_ID=?";
        return plantilla.update(eliminar,semestre.getId_semestre());
    }

    public SemestreAcademico BuscarSemestre(int semestre) {
        String buscar= "SELECT T_Semestre_Academico_ID as id_semestre,descripcion,anio FROM t_semestre_academico where T_Semestre_Academico_ID=? ";
        List<SemestreAcademico> resultado = plantilla.query(buscar,new Object[]{semestre},
                                    new BeanPropertyRowMapper(SemestreAcademico.class));
        return (resultado.size()>0)?(SemestreAcademico)resultado.get(0) :null;
    }

}
